#!/usr/bin/env python
#coding=utf-8

"""
forms
"""
from flask.ext.wtf import Form
from wtforms import TextAreaField, HiddenField, BooleanField, \
        PasswordField, SubmitField, TextField, SelectMultipleField, SelectField
from wtforms.validators import ValidationError, \
        required, email, equal_to, regexp

from planet.account.models import User


class LoginForm(Form):
    account = TextField(u"Username or email address", validators=[
                      required(message=\
                               u"You must provide an email or username")])
    password = PasswordField(u"Password")
    remember = BooleanField(u"Remember me")
    next = HiddenField()
    submit = SubmitField(u"Login")


class RegisterForm(Form):
    username = TextField(u"Username", validators=[
                         required(message=u"Username required")])

    email = TextField(u"Email address", validators=[
                      required(message=u"Email address required"),
                      email(message=u"A valid email address is required")])

    password = PasswordField(u"Password", validators=[
                             required(message=u"Password required")])

    password_again = PasswordField(u"Password again", validators=[
                                   equal_to("password", message=\
                                            u"Passwords don't match")])

    next = HiddenField()

    submit = SubmitField(u"Register")

    def validate_username(self, field):
        user = User.query.filter(User.username.like(field.data)).first()
        if user:
            raise ValidationError, u"This username is taken"

    def validate_email(self, field):
        user = User.query.filter(User.email.like(field.data)).first()
        if user:
            raise ValidationError, u"This email is taken"

class ResetPasswordEmailForm(Form):
    email = TextField(u"邮箱", validators=[
                      required(message=u"邮箱不能为空"),
                      email(message=u"邮箱不可用")])

    submit = SubmitField(u"提交")

    def validate_email(self, field):
        user = User.query.filter(User.email.like(field.data)).first()
        if not user:
            raise ValidationError, gettext(u"该邮箱不存在")

class ResetPasswordForm(Form):
    password = PasswordField(u"密码", validators=[
                             required(message=u"密码不能为空")])

    password_again = PasswordField(u"重复密码", validators=[
                                   equal_to("password", message=\
                                            u"与之前密码不匹配")])
    submit = SubmitField(u"提交")

class AccountForm(Form):
    username = TextField(u"用户名", validators=[
                         required(message=u"用户名不能为空")])

    email = TextField(u"邮箱", validators=[
                      required(message=u"Email address required"),
                      email(message=u"A valid email address is required")])
    submit = SubmitField(u"保存")

    def validate_username(self, field):
        if field.data != g.user.username:
            user = User.query.filter(User.username.like(field.data)).first()
            if user:
                raise ValidationError, gettext("This username is taken")

    def validate_email(self, field):
        if field.data != g.user.email:
            user = User.query.filter(User.email.like(field.data)).first()
            if user:
                raise ValidationError, gettext("This email is taken")



class PasswordForm(Form):
    current_password = PasswordField(u"旧密码", validators=[
                             required(message=u"Password required")])

    new_password = PasswordField(u"新密码", validators=[
                             required(message=u"Password required")])

    verify_password = PasswordField(u"新密码（重复）", validators=[
                                   equal_to("new_password", message=\
                                            u"Passwords don't match")])
    submit = SubmitField(u"保存")

    def validate_current_password(self, field):
        user, authenticated = User.query.authenticate(g.user.username,
                                                      field.data)
        if not authenticated:
            raise ValidationError, gettext("The current password is wrong")


class UserForm(Form):
    user_id = HiddenField()
    username = TextField(u"用户名", validators=[
                         required(message=u"Username required")])

    email = TextField(u"邮箱", validators=[
                      required(message=u"Email address required"),
                      email(message=u"A valid email address is required")])

    role = SelectField(u"角色", choices=[('10', u'管理员'), 
                                         ('500', u'操作员'), 
                                         ('1000', u'普通用户')])

    active = BooleanField(u"活动")

    submit = SubmitField(u"保存")

    def validate_username(self, field):
        user = User.query.get(self.user_id.data)
        if user and field.data != user.username:
            user = User.query.filter(User.username.like(field.data)).first()
            if user:
                raise ValidationError, gettext("This username is taken")

    def validate_email(self, field):
        user = User.query.get(self.user_id.data)
        if user and field.data != user.email:
            user = User.query.filter(User.email.like(field.data)).first()
            if user:
                raise ValidationError, gettext("This email is taken")

class ProfileForm(Form):
    english_first_name = TextField(u"名（拼音）", validators=[
                      required(message=\
                               u"请输入名的拼音")])

    english_last_name = TextField(u"姓（拼音）", validators=[
                      required(message=\
                               u"请输入姓的拼音")])
    
    birthday_year = TextField(u"年",validators=[
                      required(message=\
                               u"请输入出声日期")])    
    birthday_month = TextField(u"月",validators=[
                      required(message=\
                               u"请输入出声日期")])
    birthday_day = TextField(u"日",validators=[
                      required(message=\
                               u"请输入出声日期")])
    address = TextField(u"地址（拼音）",validators=[
                    required(message=\
                               u"请输入地址")])
    phone = TextField(u"电话",validators=[
                    required(message=\
                               u"请输入电话")])
    email = TextField(u"邮箱")
    submit = SubmitField(u"提交申请")


